package so.sao.code.service.web.portal;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import so.sao.auth.autoreg.annotation.ButtonResource;
import so.sao.auth.autoreg.annotation.MenuResource;
import so.sao.code.dto.request.CustomFieldCreate;
import so.sao.code.service.domain.entity.EnterpriseCustomField;
import so.sao.code.service.infrastructure.model.vo.EnterpriseCustomFieldVO;
import so.sao.code.service.service.EnterpriseCustomFieldService;
import so.sao.sso.bind.CurrentUser;
import so.sao.sso.feign.dto.UserDTO;
import springfox.documentation.annotations.ApiIgnore;

@Api(tags = "EnterpriseCustomField", description = "企业自定义字段")
@RestController
@RequestMapping("portal/customfield")
@MenuResource(perm = "customfield.*", url = "customfield")
public class EnterpriseCustomFieldController {

	@Autowired
	private EnterpriseCustomFieldService enterpriseCustomFieldService;

	/**
	 * 用户自定义字段显示
	 */
	@ApiOperation(value = "用户自定义显示", notes = "用户自定义字段后用于显示，仅透云内部用")
	@GetMapping(produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@RequiresAuthentication
	@ButtonResource(url = "view")
	@RequiresPermissions("customfield.view")
	public List<EnterpriseCustomField> showInfo(@ApiIgnore @CurrentUser UserDTO user) {
		return enterpriseCustomFieldService.findFieldsInfoS();
	}

	/**
	 * 用户自定义字段保存
	 */
	@ApiOperation(value = "自定义字段创建接口", notes = "可用于多企业添加相同的字段，不同企业id用,拼接")
//	@ApiImplicitParam(name = "customFieldCreate", value = "自定义字段创建请求", required = true, dataType = "CustomFieldCreate", paramType = "body")
	@PostMapping(produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@RequiresAuthentication
	public ResponseEntity<List<EnterpriseCustomField>> create(@ApiIgnore @CurrentUser UserDTO user,
            Integer mode, String eseId,  String fieldName,String fieldKey,  String fieldValue)
			throws IllegalAccessException, InvocationTargetException {
		return new ResponseEntity<>(enterpriseCustomFieldService.create(mode,eseId,fieldName,fieldKey,fieldValue), HttpStatus.CREATED);
	}

	/**
	 * 用户自定义字段详细信息显示
	 */
	@ApiOperation(value = "用户自定义字段详细信息显示", notes = "用户自定义字段详细信息显示")
	@ApiImplicitParam(name = "fieldKey", value = "自定义字段key", required = true, dataType = "fieldKey", paramType = "path")
	@GetMapping(value = "fieldDetail/{fieldKey}")
	@RequiresAuthentication
	public EnterpriseCustomFieldVO showDetailInfo(@ApiIgnore @CurrentUser UserDTO user,
			@PathVariable @NotNull String fieldKey) {
		return enterpriseCustomFieldService.findFieldsDetailInfo(fieldKey);
	}

	/*
	 * @ApiOperation(value = "自定义字段修改接口", notes = "可用于多企业添加相同的字段，不同企业id用,拼接")
	 * 
	 * @ApiImplicitParam(name = "enterpriseCustomFieldVO", value = "自定义字段修改请求",
	 * required = true, dataType = "EnterpriseCustomFieldVO", paramType = "body")
	 * 
	 * @PostMapping(value = "updateCustomField")
	 * 
	 * @RequiresAuthentication public ResponseEntity<List<EnterpriseCustomFieldVO>>
	 * update(@Valid @RequestBody EnterpriseCustomFieldVO enterpriseCustomFieldVO)
	 * throws IllegalAccessException, InvocationTargetException {
	 * enterpriseCustomFieldService.updateCustomField(enterpriseCustomFieldVO);
	 * return new ResponseEntity<>(HttpStatus.OK); }
	 */
	/**
	 * 用户自定义字段保存
	 */
	@ApiOperation(value = "用户自定义字段预览", notes = "用户自定义字段预览")
	@ApiImplicitParam(name = "fieldValue", value = "自定义字段预览", required = true, dataType = "fieldValue", paramType = "path")
	@GetMapping(value = "fieldResultInfo/{fieldValue}")
	@RequiresAuthentication
	public String showInfo(@PathVariable @NotNull String fieldValue) {
		return enterpriseCustomFieldService.findFieldResultInfo(fieldValue);
	}
}
